package cc.vv.btpartyjob.homepage.activity

import android.os.Message
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.global.UserInfoSharePreKey
import cc.vv.baselibrary.util.LKPrefUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.util.router.RouterIntentUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.homepage.R
import cc.vv.btpartyjob.homepage.delegate.HomePageBaseBinder
import cc.vv.btpartyjob.homepage.delegate.MyInfoActivityDelegate
import cc.vv.btpartyjob.homepage.moudel.resp.ExitLoginResponseObj
import cc.vv.btpartyjob.homepage.server.HomeHttpServer
import cc.vv.btpartyjob.jnimodule.server.OperationServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.dawning.utiming.task.api.HomeworkAPI

/**
 * 我的个人信息
 * Created by Chen.qi on 2018/10/24.
 */
class MyInfoActivity : BaseActivityMVP<MyInfoActivityDelegate>() {
    override fun initData() {
        val geRenInfo = UserInfoManageUtil.getGeRenInfo()
        viewDelegate?.setHeadImg(geRenInfo?.headPath)
        viewDelegate?.setUnsrName(geRenInfo?.name)
        viewDelegate?.setSex(geRenInfo?.sex)
        viewDelegate?.setUnsrPhone(geRenInfo?.mobile)
        viewDelegate?.setChengNuo(LKPrefUtil.getString(UserInfoSharePreKey.GE_REN_CHENG_NUO_DATA, ""))
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //退出登录
            OperationServer.operationDialog(this, handler, "退出登录", "您确定要退出登录吗？")
        }, R.id.tv_ami_exit_btn)
    }

    override fun getDelegateClass(): Class<MyInfoActivityDelegate> {
        return MyInfoActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return HomePageBaseBinder()
    }


    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {
            7 -> {
                try {
                    HomeHttpServer.exitLogin(false)
                    //退出登录
                    exitApp()
                    LKPrefUtil.clearSP(UserInfoSharePreKey.TOKEN, UserInfoSharePreKey.LOGIN_INFO,
                            UserInfoSharePreKey.TEL_PASSWORD)
                    LKPrefUtil.clearSP(UserInfoSharePreKey.YUE_PAIMING_TOP, UserInfoSharePreKey.YUE_PAIMING_BOTTOM,
                            UserInfoSharePreKey.TOTAL_PAIMING_BOTTOM, UserInfoSharePreKey.TOTAL_PAIMING_TOP)
                    LKPrefUtil.clearSP(UserInfoSharePreKey.USERINFO_DATA, UserInfoSharePreKey.GE_REN_DATA)
                    RouterIntentUtil.getInstance().intentLoginActivity(mContext)
                } catch (e: Exception) {
                }
            }

        }
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is ExitLoginResponseObj -> {
//                //退出登录
//                exitApp()
//                LKPrefUtil.clearSP(UserInfoSharePreKey.TOKEN, UserInfoSharePreKey.LOGIN_INFO,
//                        UserInfoSharePreKey.TEL_PASSWORD)
//                LKPrefUtil.clearSP(UserInfoSharePreKey.USERINFO_DATA)
//                RouterIntentUtil.getInstance().intentLoginActivity(mContext)
            }
        }

    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        when (obj) {
            is ExitLoginResponseObj -> {
                LKToastUtil.showToastShort("网络异常")
            }
        }

    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (HomeworkAPI.exitLogin == url) {
            LKToastUtil.showToastShort("网络异常")
        }
    }
}
